<template>
  <!-- 表单 -->
  <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
    <el-form-item label="姓名" prop="username">
      <el-input
        v-model="formData.username"
        style="width: 50%"
        placeholder="请输入姓名"
      />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input
        v-model="formData.mobile"
        style="width: 50%"
        placeholder="请输入手机号"
      />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker
        v-model="formData.timeOfEntry"
        style="width: 50%"
        placeholder="请选择入职时间"
      />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select
        v-model="formData.formOfEmployment"
        style="width: 50%"
        placeholder="请选择"
      >
        <!-- 下拉框聘用形式，进行循环，因为后台要传的值是id，因此value是id，laber就是显示字符串
        value值是给v-model绑定的，将来给后台用的id数字，label决定选项在页面标签显示的文字 -->
        <el-option
          v-for="item in hireType"
          :key="item.id"
          :value="item.id"
          :label="item.value"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input
        v-model="formData.workNumber"
        style="width: 50%"
        placeholder="请输入工号"
      />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input
        v-model="formData.departmentName"
        style="width: 50%"
        placeholder="请选择部门"
        @focus="focus"
        readonly
      />
      <!-- 树形部门数据 -->
      <div class="tree-box" v-if="showTree">
        <el-tree
          :data="departList"
          default-expand-all
          :props="{ label: 'name' }"
          @node-click="treeClick"
        />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker
        v-model="formData.correctionTime"
        style="width: 50%"
        placeholder="请选择转正时间"
      />
    </el-form-item>

    <el-form-item>
      <el-button @click="del">取消</el-button>
      <el-button type="primary" @click="sure">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import Employees from '@/api/constant/employees'
export default {
  name: 'EmpDialog',
  props: {
    departList: {//部门树形
      type: Array,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      // 添加表单字段
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      hireType: Employees.hireType,//聘用形式
      showTree: false,//树形结构是否隐藏
      // 添加表单的校验字段
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 取消按钮
    del () {
      this.$emit('update:showDialog', false)//弹窗关闭
      this.$refs.addForm.resetFields()//重置表单
    },
    // 确认按钮
    sure () {
      this.$refs.addForm.validate(valid => {//兜底校验
        if (valid) {
          this.$emit('addEmpFn', { ...this.formData })//展开运算符就是解决下面的细节，让他不是空值，保存一下数据
          this.$emit('update:showDialog', false)
          this.$refs.addForm.resetFields()//重置表单
          // 细节：
          // 因为上面没有await，所以清空表单的代码先执行了
          // formDate的数据对象里属性值都被重置为空字符串了
          // 把给父级用的对象拷贝一份
          // 放在setTimeout里面，让代码执行最后，最后在置空也可以
        }
      })
      this.$emit('update:showDialog', false)
    },
    // 输入框聚焦事件
    focus () {
      this.showTree = true//树形结构显示
    },
    // 让数据回显，并且关闭
    treeClick (data) {
      // console.log(data)//点击那行的对象
      if (data && data.children) {//判断是否有孩子
        return
      }
      this.formData.departmentName = data.name
      this.$nextTick(() => {
        this.showTree = false//树形结构关闭
      })
    }
  },
}
</script>

<style lang="scss" scoped></style>